<?php

/**
 * 用户信息操作类
 * @author:lin
 * @since:2012 5.21
 * 
 */
class Backstage_UserInfo {
    const TABLE_NAME_EXTEND = 'user_extend';
    
    /**
     *  用户缓存命名
     */
    const REDIS_NAME = 'userLike';

    /**
     * 缓存实例
     *
     * @var Helper_Cache_Memcache
     */
    protected $_cacheInstance;
    /**
     * db实例
     *
     * @var Helper_Cache_Memcache
     */
    protected $_dbInstance;
    /**
     * 单例对象
     */
    public static $singletonObjects;

    /**
     * 获取单例
     */
    public static function getInstance() {
        $className = __CLASS__;
        if (!isset(self::$singletonObjects[$className])) {
            self::$singletonObjects[$className] = new self();
        }

        return self::$singletonObjects[$className];
    }

    /**
     * 构造器
     *
     * @param int $uId
     */
    protected function __construct() {

        $this->_dbInstance = $this->_getMysql();
        $this->_cacheInstance = $this->_getRedis();
    }

    /*
     * 获取cache实例
     */

    public function _getCache() {

        return Common::getCache();
    }

    /*
     * 获取redis实例
     */

    public function _getRedis() {
//        $config = array ('current'=>'127.0.0.1' , 'old' =>'6380') ;
//        $redis = new Helper_Redis($config);
        $redis = new Redis();
        $redis->connect('127.0.0.1', 6380);
        return $redis;
    }

    /*
     * 获取db实例
     */

    public function _getMysql() {

        return Common::getMysql();
    }

    /**
     * 返回筛选出的用户
     * @return 
     * array(
     *      userID => userID
     *      )
     */
    public function getScreening( $dimensions ) {
        
        $allUserRedis = $this->_cacheInstance->hgetall(self::REDIS_NAME);
        $array = array();
        foreach ($allUserRedis as $key => $value) {
            $value = json_decode($value,true);
            $array[$key] = $value;
        }
        //print_r($array);
        $userRedis = array();
        
        foreach ($dimensions as $key => $value) {
            if ( $key == 'PBrand' ) {
                foreach ($array as $key1 => $value1) {
                    if ( !empty ($value1[$key]) && array_key_exists($value, $value1[$key]) ) {
                        $userRedis['PBrand'][] = $key1;
                    }
                }
                if ( empty($userRedis['PBrand']) ) {
                    $userRedis['PBrand'][] = -1;
                }
            }
            if ( $key == 'BCat' ) {
                foreach ($array as $key1 => $value1) {
                    if ( !empty ($value1[$key]) && array_key_exists($value, $value1[$key]) ) {
                        $userRedis['BCat'][] = $key1;
                    }
                }
                if ( empty($userRedis['BCat']) ) {
                    $userRedis['BCat'][] = -1;
                }
            }
            if ( $key == 'SCat' ) {
                foreach ($array as $key1 => $value1) {
                    if ( !empty ($value1[$key]) && array_key_exists($value, $value1[$key]) ) {
                        $userRedis['DHihgLevels'][] = $key1;
                    }
                }
                if ( empty($userRedis['DHihgLevels']) ) {
                    $userRedis['DHihgLevels'][] = -1;
                }
            }
            if ( $key == 'DHihgLevels' ) {
                foreach ($array as $key1 => $value1) {
                    if ( array_key_exists($key, $value1) ) {
                        $userRedis['DHihgLevels'][] = $key1;
                    }
                }
                if ( empty($userRedis['DHihgLevels']) ) {
                    $userRedis['DHihgLevels'][] = -1;
                }
            }
            if ( $key == 'DMiddleLevels' ) {
                foreach ($array as $key1 => $value1) {
                    if ( array_key_exists($key, $value1) ) {
                        $userRedis['DMiddleLevels'][] = $key1;
                    }
                }
                if ( empty ($userRedis['DMiddleLevels']) ) {
                    $userRedis['DMiddleLevels'][] = -1;
                }
            }
            if ( $key == 'DLowLevels' ) {
                foreach ($array as $key1 => $value1) {
                    if ( array_key_exists($key, $value1) ) {
                        $userRedis['DLowLevels'][] = $key1;
                    }
                }
                if ( empty($userRedis['DLowLevels']) ) {
                    $userRedis['DLowLevels'][] = -1;
                }
            }
            if ( $key == 'DOldAge' ) {
                foreach ($array as $key1 => $value1) {
                    if ( array_key_exists($key, $value1) ) {
                        $userRedis['DOldAge'][] = $key1;
                    }
                }
                if ( empty($userRedis['DOldAge']) ) {
                    $userRedis['DOldAge'][] = -1;
                }
            }
            if ( $key == 'DYouthAge' ) {
                foreach ($array as $key1 => $value1) {
                    if ( array_key_exists($key, $value1) ) {
                        $userRedis['DYouthAge'][] = $key1;
                    }
                }
                if ( empty($userRedis['DYouthAge']) ) {
                    $userRedis['DYouthAge'][] = -1;
                }
            }
            if ( $key == 'DInfancyAge' ) {
                foreach ($array as $key1 => $value1) {
                    if ( array_key_exists($key, $value1) ) {
                        $userRedis['DInfancyAge'][] = $key1;
                    }
                }
                if ( empty($userRedis['DInfancyAge']) ) {
                    $userRedis['DInfancyAge'][] = -1;
                }
            }
            if ( $key == 'DManSex' ) {
                foreach ($array as $key1 => $value1) {
                    if ( array_key_exists($key, $value1) ) {
                        $userRedis['DManSex'][] = $key1;
                    }
                }
                if ( empty($userRedis['DManSex']) ) {
                    $userRedis['DManSex'][] = -1;
                }
            }
            if ( $key == 'DWomanSex' ) {
                foreach ($array as $key1 => $value1) {
                    if ( array_key_exists($key, $value1) ) {
                        $userRedis['DWomanSex'][] = $key1;
                    }
                }
                if ( empty($userRedis['DWomanSex']) ) {
                    $userRedis['DWomanSex'][] = -1;
                }
            }
        }
        
        $val = array_slice($userRedis, 0,1);
        
        foreach ($val as $key => $value) {
            $val = $value;
        }
        
        foreach ($userRedis as $key => $value) {
            $true = array_search(-1,$value);
            if ( is_numeric($true) ){
//                echo '没有符合的用户！';
//                return false;
                throw new AppException( 9001 );
            }
            else {
                $val = array_intersect($value,$val);
            }
        }
        return $val;
    }
    /**
     * 根据用户id查找用户的email并向用户推荐产品
     */
    public function getUserInfo( $uid ,$pid){
        
        foreach( $uid as $key =>$value ){
          $userInfo = Common::getMysql()->getList( self::TABLE_NAME_EXTEND , array( 'UNameId' =>$value));  
        }
        
        foreach( $userInfo as $key => $value){
            $email[] = $value['Email'];
            /**
             * 邮箱推送
             * $value['Email'] 邮箱号码
             * $content 发送邮箱的内容
             */
            /**
             * 获取产品的信息
             */
           $content = Product_Info::getInstance()->getProuctInfoByPId( $pid );
           //print_r( $content );
            
           $result = Helper_PhpMailer::getInstance()->sendEmail( $value['Email'] , $content['PName'] , $content['PicOriginal'] , $content['Price']);
           /**
            * 手机信息发送
            */   
           //$results = Helper_Smsapi::sendSms( $value['Mobile'] , '给您推送产品：暂无');
           return $result;
          
        }
        
        
    }
    
//查找出符合维度的用户id
     public function getSuccessUser( $dimensions ) {
        
        $allUserRedis = $this->_cacheInstance->hgetall('userLike');
        
        $array = array();
        foreach ($allUserRedis as $key => $value) {
            $value = json_decode($value,true);
            $array[$key] = $value;
        }
      // print_r($array);echo '<br>';
        $userRedis = array();
        
        foreach ($dimensions as $key => $value) {
            if ( $key == 'PBrand' ) {
                foreach ($array as $key1 => $value1) {
                    if ( !empty ($value1[$key]) && array_key_exists($value, $value1[$key]) ) {
                        $userRedis['PBrand'][] = $key1;
                    }
                }
                if ( empty($userRedis['PBrand']) ) {
                    $userRedis['PBrand'][] = -1;
                }
            }
            if ( $key == 'BCat' ) {
                foreach ($array as $key1 => $value1) {
                    if ( !empty ($value1[$key]) && array_key_exists($value, $value1[$key]) ) {
                        $userRedis['BCat'][] = $key1;
                    }
                }
                if ( empty($userRedis['BCat']) ) {
                    $userRedis['BCat'][] = -1;
                }
            }
            if ( $key == 'SCat' ) {
                foreach ($array as $key1 => $value1) {
                    if ( !empty ($value1[$key]) && array_key_exists($value, $value1[$key]) ) {
                        $userRedis['DHihgLevels'][] = $key1;
                    }
                }
                if ( empty($userRedis['DHihgLevels']) ) {
                    $userRedis['DHihgLevels'][] = -1;
                }
            }
            if ( $key == 'DHihgLevels' ) {
                foreach ($array as $key1 => $value1) {
                    if ( array_key_exists($key, $value1) ) {
                        $userRedis['DHihgLevels'][] = $key1;
                    }
                }
                if ( empty($userRedis['DHihgLevels']) ) {
                    $userRedis['DHihgLevels'][] = -1;
                }
            }
            if ( $key == 'DMiddleLevels' ) {
                foreach ($array as $key1 => $value1) {
                    if ( array_key_exists($key, $value1) ) {
                        $userRedis['DMiddleLevels'][] = $key1;
                    }
                }
                if ( empty ($userRedis['DMiddleLevels']) ) {
                    $userRedis['DMiddleLevels'][] = -1;
                }
            }
            if ( $key == 'DLowLevels' ) {
                foreach ($array as $key1 => $value1) {
                    if ( array_key_exists($key, $value1) ) {
                        $userRedis['DLowLevels'][] = $key1;
                    }
                }
                if ( empty($userRedis['DLowLevels']) ) {
                    $userRedis['DLowLevels'][] = -1;
                }
            }
            if ( $key == 'DOldAge' ) {
                foreach ($array as $key1 => $value1) {
                    if ( array_key_exists($key, $value1) ) {
                        $userRedis['DOldAge'][] = $key1;
                    }
                }
                if ( empty($userRedis['DOldAge']) ) {
                    $userRedis['DOldAge'][] = -1;
                }
            }
            if ( $key == 'DYouthAge' ) {
                foreach ($array as $key1 => $value1) {
                    if ( array_key_exists($key, $value1) ) {
                        $userRedis['DYouthAge'][] = $key1;
                    }
                }
                if ( empty($userRedis['DYouthAge']) ) {
                    $userRedis['DYouthAge'][] = -1;
                }
            }
            if ( $key == 'DInfancyAge' ) {
                foreach ($array as $key1 => $value1) {
                    if ( array_key_exists($key, $value1) ) {
                        $userRedis['DInfancyAge'][] = $key1;
                    }
                }
                if ( empty($userRedis['DInfancyAge']) ) {
                    $userRedis['DInfancyAge'][] = -1;
                }
            }
            if ( $key == 'DManSex' ) {
                foreach ($array as $key1 => $value1) {
                    if ( array_key_exists($key, $value1) ) {
                        $userRedis['DManSex'][] = $key1;
                    }
                }
                if ( empty($userRedis['DManSex']) ) {
                    $userRedis['DManSex'][] = -1;
                }
            }
            if ( $key == 'DWomanSex' ) {
                foreach ($array as $key1 => $value1) {
                    if ( array_key_exists($key, $value1) ) {
                        $userRedis['DWomanSex'][] = $key1;
                    }
                }
                if ( empty($userRedis['DWomanSex']) ) {
                    $userRedis['DWomanSex'][] = -1;
                }
            }
        }
       
        $val = array_slice($userRedis, 0,1);
        foreach ($val as $key => $value) {
            $val = $value;
        }
        
        foreach ($userRedis as $key => $value) {
            $true = array_search(-1,$value);
            if ( is_numeric($true) ){
                return false;
            }
            else {
                $val = array_intersect($value,$val);
            }
        }
        //print_r($val);
        return $val;
    }

}

?>
